package Com.Hupochuan.Uil.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Com.Tester.Tester;

public class BaseFilter implements Filter {
	
	private String encoding = "utf-8"; 
	
	/** 服务器调用一次init为服务准备过滤器 */
	public void destroy() {
		//打印测试
		Tester.print("服务器启动Base过滤器");
	}

	/*
	 * 执行实际的过滤工作 
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		//获得请求信息对象 获得响应信息对象 设置编码类型
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		request.setCharacterEncoding(this.encoding);
		
		//获得请求URL路径
		String requestURI = request.getRequestURI();
		
		//打印测试
		Tester.print("请求路过BaseFilter过滤器,请求:"+requestURI);
		
		if(requestURI.endsWith(".jsp")){
			response.sendError(404);
			return;
		}
		else{
			filterChain.doFilter(request, response);
		}
	}

	/** 指出过滤器已结束服务 */
	public void init(FilterConfig config) throws ServletException {
		//打印测试
		Tester.print("第一过滤器已结束服务");
		//encoding = config.getInitParameter("Encoding");
	}
}
